<?php

namespace admin\controllers;

use admin\components\AdminController;
use app\models\Activity;
use app\models\ActivityApply;
use yii;

class ActivityController extends AdminController {

    public $name = '活动管理';
    public $type = Activity::TYPE_ACTIVITY;

    public function actionIndex() {
        $searchModel = NULL;
        $query = Activity::find()->with(['user'])->where(['type' => $this->type]);
        $query->orderBy('is_top desc, status asc, id desc');
        $dataProvider = $this->search($query, new Activity());
        return $this->render(compact('searchModel', 'dataProvider', 'status'));
    }

    public function actionApplyList() {
        $aid = $this->get('id');

        $searchModel = NULL;
        $query = ActivityApply::find();
        $query->andWhere(['aid' => $aid]);

        $dataProvider = $this->search($query, $searchModel);
        return $this->render(compact('searchModel', 'dataProvider'), '/activity/apply');
    }

    public function actions() {
        return [
            'view' => [
                'class' => 'admin\components\ViewAction',
                'modelClass' => Activity::className(),
            ],
            'delete' => [
                'class' => 'admin\components\DeleteAction',
                'modelClass' => Activity::className(),
                'status' => Activity::STATUS_DELETE
            ]
        ];
    }

    public function actionAllow($id) {
        $model = Activity::findOne($id);
        if (!$model || !$model->getIsVerify()) {
            return $this->error();
        }

        $model->status = Activity::STATUS_PENDING;
        return $model->save(FALSE);
    }

    public function actionDeny($id) {
        $model = Activity::findOne($id);
        if (!$model || !$model->getIsVerify()) {
            return $this->error();
        }

        $model->status = Activity::STATUS_VERIFY_DENY;
        $model->remark = $this->get('remark');
        return $model->save(FALSE);
    }

    public function actionUndo($id) {
        $model = Activity::findOne($id);
        if (!$model || !$model->getIsDelete()) {
            return $this->error();
        }

        if ($model->end_time < time()) {
            $model->status = Activity::STATUS_END;
        } else {
            $model->status = Activity::STATUS_VERIFY;
        }
        return $model->save(FALSE);
    }

    public function actionSetNew($id){
        $model = Activity::findOne($id);
        if (!$model || $model->getIsDelete()) {
            return $this->error();
        }

        $model->is_new = $model->getIsNew() ? Activity::STATUS_NO : Activity::STATUS_YES;
        return $model->save(FALSE);
    }

    public function actionSetHot($id){
        $model = Activity::findOne($id);
        if (!$model || $model->getIsDelete()) {
            return $this->error();
        }

        $model->is_hot = $model->getIsHot() ? Activity::STATUS_NO : Activity::STATUS_YES;
        return $model->save(FALSE);
    }

    public function actionSetTop($id){
        $model = Activity::findOne($id);
        if (!$model || $model->getIsDelete()) {
            return $this->error();
        }

        $model->is_top = $model->getIsTop() ? Activity::STATUS_NO : Activity::STATUS_YES;
        return $model->save(FALSE);
    }
}
